/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 扩展一个Util模块
 **/

layui.define('jquery', function (exports) {

    var $ = layui.$;
    var utils = {};

    utils.onresize = function (func) {
        if (typeof func === 'function') {
            $(window).on('resize', func);
            func();
        }
    }


    utils.getCheckBoxVal = function (parent) {
        var elems, values = [];
        if (parent == null)
            elems = $('.layui-form-checked');
        else
            elems = $(parent).find('.layui-from-checked');
        $.each(elems, function (index) {
            var elem = elems.eq(index);
            var input = elem.prev('input');
            values.push(input.val());
        });
        return values;
    }

    // 设置cookie
    utils.setCookie = function (name, value, days = 1) {
            var date = new Date();
            date.setDate(date.getDate() + days);
            document.cookie = name + '=' + value + ';expires=' + date;
      }

    //获取cookie
    utils.getCookie = function (name) {
        var arr = document.cookie.replace(/\s/g, "").split(';');
        for (var i = 0; i < arr.length; i++) {
            var tempArr = arr[i].split('=');
            if (tempArr[0] == name) {
                return decodeURIComponent(tempArr[1]);
            }
        }
        return '';
    }

    // 删除cookie
      utils.removeCookie = function (name) {             
                var date = new Date();
            date.setDate(date.getDate() - 1);
            document.cookie = name + '=' + 1 + ';expires=' + date;
      }
 
      // 检查是否含有某cookie
      utils.hasCookie = function (name) {
            return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(name).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
      }
 
      // 获取全部的cookie列表
      utils.getAllCookie = function () {
            var cookieArr = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
            for (var nIdx = 0; nIdx < cookieArr.length; nIdx++) {
            cookieArr[nIdx] = decodeURIComponent(cookieArr[nIdx]);
        }
            return cookieArr;
      }

    //输出test接口
    exports('utils', utils);
});    